/*
 *   Copyright 2008 Francois Fernandes
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.googlecode.opendoclibrary.connector.internal;

import java.util.HashMap;
import java.util.Map;

public class VMConnectorManager
{
    private static VMConnectorManager instance;

    public synchronized static VMConnectorManager getInstance()
    {
        if ( instance == null )
        {
            instance = new VMConnectorManager();
            // touch the ServerInstanceManager to ensure running server instances
            ServerInstanceManager.getInstance().ensureLoaded();
        }
        return instance;
    }

    private Map<String, VMConnector> activeConntectors;

    private VMConnectorManager()
    {
        // singleton, no external instantiation
        activeConntectors = new HashMap<String, VMConnector>();
    }

    public void addConnector( String name, VMConnector conn )
    {
        if ( name == null )
        {
            throw new IllegalArgumentException( "name may not be null" );
        }
        if ( conn == null )
        {
            throw new IllegalArgumentException( "conn may not be null" );
        }
        activeConntectors.put( name, conn );
    }

    public VMConnector getConnector( String name )
    {
        return activeConntectors.get( name );
    }

    public void removeConnector( String name, VMConnector conn )
    {
        if ( name == null )
        {
            throw new IllegalArgumentException( "name may not be null" );
        }
        if ( conn == null )
        {
            throw new IllegalArgumentException( "conn may not be null" );
        }
        if ( conn == activeConntectors.get( name ) )
        {
            activeConntectors.remove( name );
        }
        else
        {
            throw new IllegalArgumentException( "illegal connector and name combination." );
        }
    }
}
